package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ArticleMapper;
import com.heima.article.service.ArticleService;
import com.heima.article.service.IApArticleConfigService;
import com.heima.article.service.IApArticleContentService;
import com.heima.common.constants.ArticleConstants;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.ArticleHomeDto;
import com.heima.model.article.pojos.ApArticle;
import com.heima.model.article.pojos.ApArticleConfig;
import com.heima.model.article.pojos.ApArticleContent;
import com.heima.model.common.dtos.ResponseResult;
import net.sf.jsqlparser.statement.drop.Drop;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * TODO
 *
 * @Author mr.lin
 * @Date 2025/1/24 13:52
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, ApArticle> implements ArticleService {


    @Autowired
    private ArticleMapper articleMapper;

    // 单页最大加载的数字
    private final static short MAX_PAGE_SIZE = 50;

    /**
     * 首页加载文章列表(三合一实现方法) 加载当前 加载最新 加载更多
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult load(ArticleHomeDto dto, Short type) {
        //1.非空判断
        if (dto.getSize() == null) {
            dto.setSize(10);
        }
        //单页最大加载判断
        Integer size = Math.min(dto.getSize(), MAX_PAGE_SIZE);
        dto.setSize(size);
        if (dto.getTag() == null) {
            dto.setTag(ArticleConstants.DEFAULT_TAG);
        }
        if (dto.getMinBehotTime() == null) {
            dto.setMinBehotTime(new Date());
        }

        if (dto.getMaxBehotTime() == null) {
            dto.setMaxBehotTime(new Date());
        }
        //2.调用mapper
        List<ApArticle> apArticles = articleMapper.loadArticleList(dto, type);
        return ResponseResult.okResult(apArticles);
    }

    @Autowired
    private IApArticleContentService apArticleContentService;

    @Autowired
    private IApArticleConfigService apArticleConfigService;

    /**
     * 保存文章接口 涉及三张表
     *
     * @param dto
     * @return
     */
    @Override
    @Transactional
    public ResponseResult saveArticle(ArticleDto dto) {

        ApArticle apArticle = new ApArticle();
        BeanUtils.copyProperties(dto, apArticle);

        //1.判断是否有文章id
        if (apArticle.getId() == null) {
            //新增操作
            //保存文章信息表
            save(apArticle);
            //保存文章配置表
            ApArticleConfig apArticleConfig = new ApArticleConfig(apArticle.getId());
            apArticleConfigService.save(apArticleConfig);
            //保存文章内容表
            ApArticleContent apArticleContent = new ApArticleContent();
            apArticleContent.setArticleId(apArticle.getId());
            apArticleContent.setContent(dto.getContent());
            apArticleContentService.save(apArticleContent);
        } else {
            //修改操作
            //修改文章信息表
            ApArticle byId = getById(apArticle.getId());
            if (byId != null) {
                updateById(apArticle);
            }
            //修改文章内容表
            ApArticleContent apArticleContent = apArticleContentService.getOne(Wrappers.<ApArticleContent>lambdaQuery().eq(ApArticleContent::getArticleId, apArticle.getId()));
            if (apArticleContent != null) {
                apArticleContentService.lambdaUpdate().eq(ApArticleContent::getArticleId, apArticle.getId())
                        .set(ApArticleContent::getContent, dto.getContent())
                        .update();
            }

        }
        return ResponseResult.okResult(apArticle.getId());
    }


}
